package com.lp;

import com.lp.bo.TbRealMonData03BO;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;

public class ApiTest {

    public static void main(String[] args) {
        // 目标 URL
        String url = "https://example.com/v1/data/perceive/receiveRealAlarmDataList";  // 替换为实际的接口地址

        TbRealMonData03BO tbRealMonData03BO = new TbRealMonData03BO();
        tbRealMonData03BO.setSt_data_id("bfcc9053-3e1e-440f-9e6c-e9d6b5cc5e9b");
        tbRealMonData03BO.setTarget_code("300000042003G0");
        tbRealMonData03BO.setSt_data(0.2);
        tbRealMonData03BO.setCollect_time(new Timestamp(new Date().getTime()));
        // 构造请求数据
        String jsonBody = "{\n" +
                "  \"data\": [\n" +
                "    {\n" +
                "      \"stDataId\": \"bfcc9053-3e1e-440f-9e6c-e9d6b5cc5e9b\",\n" +
                "      \"targetCode\": \"300000042003G0002YW001\",\n" +
                "      \"stData\": \"0\",\n" +
                "      \"collectTime\": \"2024-06-30 23:59:59\",\n" +
                "      \"alarmtype\": \"alarmlo:alarm\",\n" +
                "      \"threshold\": \"0\"\n" +
                "    }\n" +
                "  ]\n" +
                "}";

        // 初始化 HttpClient
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // 创建 POST 请求
            HttpPost httpPost = new HttpPost(url);

            // 设置请求头
            httpPost.setHeader("Content-Type", "application/json;charset=utf8");
            httpPost.setHeader("UniTrust-Token", "A5ewTio3NX20220424150035407");
            httpPost.setHeader("UniTrust-AppId", "06e9f82867b44277b296801e160b876e");

            // 设置请求体
            StringEntity entity = new StringEntity(jsonBody);
            httpPost.setEntity(entity);

            // 发送请求并获取响应
            try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
                // 获取响应状态码
                int statusCode = response.getStatusLine().getStatusCode();
                System.out.println("Response Status Code: " + statusCode);

                // 获取响应内容
                HttpEntity responseEntity = response.getEntity();
                String responseString = EntityUtils.toString(responseEntity);
                System.out.println("Response Body: " + responseString);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
